In this short lesson, we'll see how to register system wide keyboard shortcuts for an electron up.
Now, in order to understand this, we must first cover the concept of an accelerator string accelerators,
having its own dedicated section.
And the docs isn't a module, but rather a standard naming convention for defining key combinations.
For example, the most basic of accelerator strings, Kay, would simply mean when the key is pressed
or in combination form, we could have shift plus key meaning shift was pressed and held and then carry
the order being very important here.
So pretty straightforward then.
The second part of registering a systemwide shortcut is electrons global shortcut module.
We use this module to register an accelerator string to an associated function for our app.
Lets see this in action.
Step one, as always, is to require the global shortcut module from electron global shortcut.
Then in create a window as we have to wait for the app to be ready before registering an accelerator
called Global Shortcut Register with the first argument being the accelerator string.
Make this G for now and the second a callback to fire when the shortcut is pressed in which I'll just
log out.
User pressed G.
Save this.
I'll press G check the terminal, and there we see the log message.
We could, however, have done exactly this by listening on the Web content, but the whole point of
global shortcut is that it's global or system wide, meaning our app simply has to be running and doesn't
need to be in focus.
So remaining in the terminal window oppressed again and we get the same message.
This being a truly global shortcut, then having such access to system key presses does, however,
come with a bit of responsibility.
You'll have to keep in mind that another application may already be using your accelerator.
And unfortunately, there's nothing we can do about this as we are not allowed to know what shortcuts
other applications have registered that would pose a serious security risk.
So definitely something to keep in mind.
That said, using combination accelerators or key combinations will make your application shortcuts
a lot more unique and unlikely to clash with other apps.
For example, we can change this accelerator to command G instead.
Or if you're on Windows Control G, which we write as command or control plus G this command or control
being a cross platform accelerator string for exactly this purpose.
Logging out user pressed G with a combination key save our immediately switch to the terminal old command
and press G and there we have it, the accelerator is matched as soon as the key is also pressed and
logs out the message.
So that's essentially all there is to using accelerators with the global shortcut module.
We do have access to a few other methods, like checking whether an accelerator is already registered
for our app.
Only, of course, on registering a single accelerator or simply removing all shortcuts for our app.
I'll quickly demonstrate unregister in the Shortcuts callback immediately after logging out our message
call global shortcut unregister passing in the same accelerator string.
So registering command G.
And then when it fires and registering it, making this a one off shortcut safe back to the terminal
press command.
Gee, we see the log message, but when I press it again, nothing as command or control plus G is no
longer registered.
That wraps up this lesson.
A very small and simple module, but very useful.